local LushAdvanceLoot_LootInfo = {};

function LushCreateLootFrameText(FrameID, ButtonName, PassStr, Location)
	local LootFrame = _G["GroupLootFrame" .. FrameID];
	local Button 	= _G["GroupLootFrame" .. FrameID .. ButtonName];
	
	local font = Button:CreateFontString(nil, "OVERLAY", "NumberFont_Shadow_Med");
	font:SetHeight(10);
	font:SetPoint(Location, 0, 0);
	font:SetJustifyH("LEFT");
	font:SetText("0");

	-- Create the button table
	if not LushAdvanceLoot_LootInfo[LootFrame:GetID()] then
		LushAdvanceLoot_LootInfo[LootFrame:GetID()] = {};
		LushAdvanceLoot_LootInfo[LootFrame:GetID()].buttons = {};
	end

	-- Add to the button table
	LushAdvanceLoot_LootInfo[LootFrame:GetID()].buttons[ButtonName] = {};
	LushAdvanceLoot_LootInfo[LootFrame:GetID()].buttons[ButtonName].font 	= font;
	LushAdvanceLoot_LootInfo[LootFrame:GetID()].buttons[ButtonName].num 	= 0;
	LushAdvanceLoot_LootInfo[LootFrame:GetID()].buttons[ButtonName].fndstr	= PassStr;	
	
	if not LushAdvanceLoot_LootInfo[LootFrame:GetID()].MoneyFrame then
		LootFrame:SetHeight(LootFrame:GetHeight() + 9); -- DefSize + MoneyFrame
		
		-- Create the Money Frame
		local MF = CreateFrame('Frame', 'LushAdvanceLootMoneyFrame' .. LootFrame:GetID(), LootFrame, 'SmallMoneyFrameTemplate');
		MF:ClearAllPoints();
		MF:SetPoint("BOTTOMLEFT", LootFrame, "BOTTOMLEFT", 12, 11);
		MF:Show();

		-- Get rid of the default functionality
		MF:UnregisterAllEvents();
		
		_G[ MF:GetName() .. "CopperButton" ]:SetScript("OnClick", nil);
		_G[ MF:GetName() .. "SilverButton" ]:SetScript("OnClick", nil);
		_G[ MF:GetName() .. "GoldButton"   ]:SetScript("OnClick", nil);
		
		MF:SetScript("OnShow", nil);
		MF:SetScript("OnHide", nil);
		
		MoneyFrame_Update(MF, 0);
		
		LushAdvanceLoot_LootInfo[LootFrame:GetID()].MoneyFrame = MF;
	end
end

function LushGroupLootFrame_OnShow(self)
	for buttonname, button in pairs( LushAdvanceLoot_LootInfo[ self:GetID() ].buttons ) do
		button.font:SetText("");
		button.num = 0;

		-- Update Money
		local _, _, _, _, _, _, _, _, _, _, itemSellPrice = GetItemInfo(GetLootRollItemLink(self.rollID));
		if itemSellPrice then
			MoneyFrame_Update(LushAdvanceLoot_LootInfo[ self:GetID() ].MoneyFrame, itemSellPrice);
		end
	end
end

function LushGroupLootFrame_OnEvent(self, event, ...)
	if ( event == "CHAT_MSG_LOOT" ) then
		if (self.rollID) then
			local texture, name, count, quality, bindOnPickUp, canNeed, canGreed, canDisenchant = GetLootRollItemInfo(self.rollID);
			local arg1 = ...;
			if (arg1 and name) then			
				if ( arg1:find( name, nil, true) ) then
					
					for buttonname, button in pairs( LushAdvanceLoot_LootInfo[ self:GetID() ].buttons ) do
						if ( arg1:find( button.fndstr ) ) then
							button.num = button.num + 1;
							button.font:SetText("" .. button.num);
							break;
						end
					end

				end
			end
		end
	end
end

for lfid=1, 4 do
	-- Create Text Fields
	LushCreateLootFrameText(lfid, "RollButton", 		"需求",			"RIGHT");
	LushCreateLootFrameText(lfid, "GreedButton", 		"貪婪",		"RIGHT");
	LushCreateLootFrameText(lfid, "DisenchantButton", 	"分解",	"RIGHT");
	LushCreateLootFrameText(lfid, "PassButton", 		"放棄",		"CENTER");
	
	-- Hook Scripts.
	local LootFrame = _G["GroupLootFrame" .. lfid];
	LootFrame:HookScript("OnShow", 	LushGroupLootFrame_OnShow);
	LootFrame:HookScript("OnEvent", LushGroupLootFrame_OnEvent);
	
	-- Register Events.
	LootFrame:RegisterEvent("CHAT_MSG_LOOT");
end